package com.jsyl.lwbk.business.service.impl;

import com.jsyl.lwbk.business.mapper.CenterMapper;
import com.jsyl.lwbk.business.domain.Center;
import com.jsyl.lwbk.business.service.ICenterService;
import com.jsyl.lwbk.system.domain.SysUser;
import com.jsyl.lwbk.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CenterServiceImpl implements ICenterService {

    @Autowired
    CenterMapper centerMapper;
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<Center> selectCenterByUser(SysUser user) {
        List<Center> list;
        Integer count = sysUserMapper.findRoleIdByUserId(user.getUserId());
        // 管理员显示全部
        if (count > 0) {
            list = centerMapper.selectAllCenter();
        } else {
            list = centerMapper.selectCenterByUserId(user.getUserId());
        }
        return list;
    }

    @Override
    public List<Center> selectIndexCenterByUser(SysUser user) {
        List<Center> list = new ArrayList<>();
        Center center = new Center();
        center.setName("----请选择中心----");
        list.add(center);
        Integer count = sysUserMapper.findRoleIdByUserId(user.getUserId());
        // 管理员显示全部
        if (count > 0) {
            list.addAll(centerMapper.selectAllCenter());
        } else {
            if (checkAllCenter(user.getUserId())) {
                list.addAll(centerMapper.selectAllCenter());
            } else {
                list.addAll(centerMapper.selectCenterByUserId(user.getUserId()));
            }
        }
        return list;
    }

    /**
     * 判断是否拥有所有中心，傻逼的原来系统有一个 -1 为全部中心的
     *
     * @param userId
     * @return
     */
    private Boolean checkAllCenter(Long userId) {
        Integer count = centerMapper.selectHaveAllCenter(userId);
        if (count > 0) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

}
